package r_Inst;
import mips.Decoder;
import mips.PC;
import mips.Register;

public class Beq {

	public static void beq(int dest, int sc1, int sc2) {
		int oldPC = PC.pc;
		if (Register.registers[sc1] == Register.registers[sc2])
			PC.pc += (dest*4);

		System.out.println("PC value now is : " +Decoder.toBinary(oldPC));

		System.out.println("PC value :"+ Decoder.toBinary(oldPC)+" sent to The instruction memory to read address..");

		System.out.println("Instuction memory sending Instruction[25:21] : "+Decoder.AddzerostoLeft(Decoder.toBinary(sc1)+"",5)+" to Registers @ read Register1");
		System.out.println("Instuction memory sending Instruction[20:16]: "+Decoder.AddzerostoLeft(Decoder.toBinary(sc2)+"",5)+"  to Registers @ read Register2");

		System.out.println("Register number  read now is : " + Decoder.AddzerostoLeft(Decoder.toBinary(sc1)+"",5));
		System.out.println("Register2 number read now is : " + Decoder.AddzerostoLeft(Decoder.toBinary(sc2)+"",5));

		System.out.println("Instruction Memory sends Instruction[15:0] to the Sign-extend unit to map it to 32 bits: " + Decoder.AddzerostoLeft(Decoder.toBinary(dest) +"",16));
		System.out.println("The sign-extended offset: " + Decoder.signExtend(Decoder.AddzerostoLeft(Decoder.toBinary(dest) +"",16), 32));
		System.out.println("Then the offset gets shifted left by two:" + Decoder.signExtend(Decoder.AddzerostoLeft(Decoder.toBinary(dest * 4) +"",16), 32));
		System.out.println("The address of the next instruction is added to the offset: " + Decoder.signExtend(Decoder.AddzerostoLeft(Decoder.toBinary(dest * 4 + (oldPC + 4)) +"",16), 32));


		System.out.println("Instuction memory sending Instruction[31:26] to CONTROL,Instruction[31:26] having : "+ "000100");
		System.out.println("CONTROL sends ALUOP : 001 to ALU CONTROL");

		System.out.println("ALU CONTROL sends 0110 to the ALU");

		System.out.println("Registers sends first source value : "+ Decoder.toBinary(Register.registers[sc1]) + " Second Source value : " + Decoder.toBinary(Register.registers[sc2]) + "  to ALU");
		System.out.println("ALU is doing the sub operating now...");

		PC.pc +=4;

		if (Register.registers[sc1] == Register.registers[sc2]) {
			System.out.println("XOR operatin between the Zero wire (with value 0) from the ALU and bne wire from the Control (with value 1). 0 ^ 1 = 1");
			System.out.println("And operation is done now with the reuslt from the xor gate (with value 1) with branch wire from the Control unit (with value 1). 1 & 1 = 1");
			System.out.println("The result from the and gate selects 1 which is the operated branch address: " + Decoder.AddzerostoLeft(Decoder.toBinary(PC.pc) + "", 32));
			System.out.println("the next mux selects the previously selected value to be written back to the pc with a signal from the Control");

		}
		else {
			System.out.println("XOR operatin between the Zero wire (with value 1) from the ALU and bne wire from the Control (with value 1). 1 ^ 1 = 0");
			System.out.println("And operation is done now with the reuslt from the xor gate (with value 10) with branch wire from the Control unit (with value 1). 0 & 1 = 0");
			System.out.println("The result from the and gate selects 0 which is (PC + 4): " + Decoder.AddzerostoLeft(Decoder.toBinary(PC.pc) + "", 32));
			System.out.println("the next mux selects the previously selected value to be written back to the pc with a signal from the Control");

		}

		System.out.println("Mux sent back to PC");
		System.out.println("New value of PC now is : " + Decoder.toBinary(PC.pc));
  	System.out.println("------------------------------------");
	}

}